//
//  BHCommonPicker.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/16.
//

import UIKit

class BHHourMinPicker: UIView {
    
    var didPickerChoseTimeActBlock:((String) -> Void)?
    

    var containV:UIView = {
        let view = UIView(frame: CGRect(x: 0, y: UIScreen.height-240, width: UIScreen.width, height: 240))
        view.backgroundColor = UIColor.white
        return view
    }()
    var backgroundView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        return view
    }()
    
    lazy var picker: UIPickerView = {
        let pick = UIPickerView.init(frame: CGRect(x: 0, y: 24, width: UIScreen.width, height: 216))
        pick.delegate = self
        pick.dataSource = self
        return pick
    }()
        
    // 时间
    var hours: [String] = []
    var min: [String] = []
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        self.configTime()
        
        self.backgroundView.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
        
        self.addSubview(self.backgroundView)
        self.addSubview(self.containV)
        self.containV.addSubview(self.picker)
        
        let cancel = UIButton(frame: CGRect(x: 0, y: 10, width: 70, height: 20))
        let sure = UIButton(frame: CGRect(x: UIScreen.width - 80, y: 10, width: 70, height: 20))
        cancel.setTitle("取消", for: .normal)
        cancel.titleLabel?.font = .systemFont(ofSize: 15, weight: .medium)
        sure.setTitle("确认", for: .normal)
        sure.titleLabel?.font = .systemFont(ofSize: 15, weight: .medium)
        cancel.setTitleColor(.color_999999, for: .normal)
        sure.setTitleColor(.color_34CE98, for: .normal)
        cancel.addTarget(self, action: #selector(self.onClickCancel), for: .touchUpInside)
        sure.addTarget(self, action: #selector(self.onClickSure), for: .touchUpInside)
        
        self.containV.addSubview(cancel)
        self.containV.addSubview(sure)

        self.containV.addCorner(conrners: [.topLeft,.topRight], radius: 12)
        
        self.containV.transform =  CGAffineTransform(translationX: 0, y: (self.containV.frame.height))

    }
  
    private func configTime() {
        for i in 0...23 {
            if i < 10 {
                if i == 0 {
                    self.hours.append("00")
                }else {
                    self.hours.append("0\(i)")
                }
            }else {
                self.hours.append("\(i)")
            }
        }
        
        for i in 0...59 {
            if i < 10 {
                if i == 0 {
                    self.min.append("00")
                }else {
                    self.min.append("0\(i)")
                }
            }else {
                self.min.append("\(i)")
            }
        }
    }
    func show()  {
        self.isHidden = false
        UIView.animate(withDuration: 0.2, animations: {
            /// transform初始化
            self.containV.transform = CGAffineTransform.identity
        }, completion: { (_) in
           
        })
    }
    
    func hide()  {
        UIView.animate(withDuration: 0.25, animations: {
            /// datepicker向下推回
            self.containV.transform =  CGAffineTransform(translationX: 0, y: (self.containV.frame.height))
        }) { (_) in
            self.isHidden = true
        }
    }
    
    
    @objc func onClickSure() {
        let hoursText = self.hours[self.picker.selectedRow(inComponent: 0)]
        let minText = self.min[self.picker.selectedRow(inComponent: 1)]
        let dateString = "\(hoursText):\(minText)"
        
        self.didPickerChoseTimeActBlock?(dateString)
        self.hide()
    }
    // MARK: onClick
    @objc func onClickCancel() {
        self.hide()
    }

}
// MARK: - PickerViewDelegate
extension BHHourMinPicker:UIPickerViewDelegate,UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var label = view as? UILabel
        if label == nil {
            label = UILabel.init()
            label?.adjustsFontSizeToFitWidth = true
            label?.textAlignment = .center
            label?.backgroundColor = .clear
            label?.font = .systemFont(ofSize: 14, weight: .medium)
        }
        label?.text = self.pickerView(picker, titleForRow: row, forComponent: component)
        return label!
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return self.hours.count
        } else {
            return self.min.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return UIScreen.width / 2.0
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return self.hours[row]
        }else {
            return self.min[row]
        }
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       
    }
}
